package pro.lepers.portfolio.view.contents.portfolio {
	import caurina.transitions.Equations;
	import caurina.transitions.Tweener;

	import fr.digitas.flowearth.command.IBatchable;
	import fr.digitas.flowearth.event.NodeEvent;
	import fr.digitas.flowearth.ui.layout.ILayoutItem;

	import pro.lepers.portfolio.view.contents.portfolio.data.ProjectDatas;
	import pro.lepers.portfolio.view.contents.portfolio.thumb.ContentHolder;

	import flash.display.Sprite;
	import flash.events.Event;

	/**
	 * @author plepers
	 */
	public class ProjectItem extends Sprite implements ILayoutItem {
		

		public static const THW : int = 512;
		public static const THH : int = 320;
		public static const COLLAPSE_SCALE : Number= .33;

		public static const COLLAPSE_HEIGHT : int = int( THH * COLLAPSE_SCALE );
		public static const DIFF_HEIGHT : int = int( THH * ( 1 - COLLAPSE_SCALE ) );

		public static const OPEN_EASE : Function = Equations.easeInOutQuad;
		public static const CLOSE_EASE : Function = Equations.easeInQuad;
		public static const OPEN_EASE_TIME : Number= .25;
		public static const CLOSE_EASE_TIME : Number= .25;
		
		public function get projectDatas () : ProjectDatas {
			return _pDatas;
		} 
		
		public function get h() : Number {
			return _h;
		}
		
		public function set h(h : Number) : void {
			_h = h;
			_content.y = _h>>1;
			dispatchEvent( new Event( Event.RESIZE ) );
		}
		
		public function ProjectItem () {
			cacheAsBitmap = true;
			
		}
		

		public function dispose () : void {
			_pDatas.node.removeEventListener( NodeEvent.CHANGE, onNodeChange, true );
			_pDatas.node.removeEventListener( NodeEvent.CHILD_CHANGE , onChildChange, true );
			_pDatas = null;
			_content.dispose();
		}

		public function setDatas ( pDatas : ProjectDatas ) : void {
			_pDatas = pDatas;
			_build( );
			_desc.setDatas( pDatas );
			
			_pDatas.node.addEventListener( NodeEvent.CHANGE, onNodeChange );
			_pDatas.node.addEventListener( NodeEvent.CHILD_CHANGE , onChildChange );
			onNodeChange();
			onChildChange();
		}
		
		public function getMainLoadable() : IBatchable {
			return _content.getMainLoadable();
		}

		public function getContentLoadables() : IBatchable {
			return _content.getLoadables();
		}

		
		
		public function getWidth () : Number {
			return 0;
		}
		
		public function getHeight () : Number {
			return Math.round( _h );
		}
		
		public function next() : void {
			_pDatas.contentsProvider.next();
		}

		public function prev() : void {
			_pDatas.contentsProvider.prev();
		}
		
		public function open() : void {
			if( _opened ) return;
			_opened = true;
			Tweener.removeTweens( _desc, "x", "y", "hiding" );
			cacheAsBitmap = getThumb().locked = false;
			Tweener.addTween( this, { h : THH,  time : OPEN_EASE_TIME, transition : OPEN_EASE } );
			Tweener.addTween( _desc , { x : THW + 30 - THW*COLLAPSE_SCALE, y : DIFF_HEIGHT>>1, time : CLOSE_EASE_TIME, transition : OPEN_EASE } );
			Tweener.addTween( _desc , { hiding : 1, time : OPEN_EASE_TIME, transition : Equations.easeInQuad } );
			//_content.open();
		}

		public function close() : void {
			if( ! _opened ) return;
			_opened = false;
			Tweener.removeTweens( _desc, "x", "y", "hiding" );
			cacheAsBitmap = getThumb().locked = false;
			Tweener.addTween( this, { h : COLLAPSE_HEIGHT, time : CLOSE_EASE_TIME, transition : CLOSE_EASE, onComplete : closeComplete } );
			Tweener.addTween( _desc , { x : THW*COLLAPSE_SCALE + 30, y : 0, time : CLOSE_EASE_TIME, transition : CLOSE_EASE } );
			Tweener.addTween( _desc , { delay : .5, hiding : 0, time : CLOSE_EASE_TIME, transition : CLOSE_EASE } );
			//_content.close();
		}

//		private function openComplete () : void {
//			getThumb().locked = true;
//		}

		private function closeComplete () : void {
			cacheAsBitmap = true;
		}

		
		private function _build () : void {
			
			_content = new ProjectContent( _pDatas.contentsProvider );
			addChild( _content );
			_content.y = _h>>1;
			_content.x = ( THW * COLLAPSE_SCALE ) >>1;
			
			getThumb().locked = true;
			
			_desc = new ProjectDescription();
			_desc.x = THW*COLLAPSE_SCALE + 40;
			_desc.alpha = .4;
			addChild( _desc );
		}
		

		private function onNodeChange (event : NodeEvent = null ) : void {
			var active : Boolean = _pDatas.node.isActive();
			if( _active == active ) return;
			_active = active;
			cacheAsBitmap = false;
			if( _active ) {
				Tweener.addTween( _desc, { alpha : 1, time : .8, onComplete : hilightComplete } );
				//Tweener.addTween( getThumb(), { altitude : 2, time : 1.6 } );
			} 
			else {
				Tweener.addTween( _desc, { alpha : .2, time : .6, onComplete : hilightComplete  } );
				//Tweener.addTween( getThumb(), { altitude : 1, time : 1.4} );
			}
		}

		private function hilightComplete () : void {
			cacheAsBitmap = true;
		}

		
		private function onChildChange(event : NodeEvent = null ) : void {
			if( _pDatas.node.getCurrentChild() != null )
				open();
			else
				close();
		}
		
		private function getThumb() : ContentHolder {
			return _content.mainThumb;
		}

		private var _active : Boolean = false;
		
		private var _pDatas : ProjectDatas;

		private var _desc : ProjectDescription;
		
		private var _opened : Boolean = false;
		
		private var _h : Number = COLLAPSE_HEIGHT;
		
		private var _content : ProjectContent;
		
	}
}
